home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5072 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.6 KB  |  102 lines

  1. Path: telepost.no!usenet
  2. From: Carsten Arnholm <ca@sesam.dnv.no>
  3. Newsgroups: comp.lang.fortran,comp.lang.c++
  4. Subject: Re: How to link Fortran with c++  ??
  5. Date: 2 Feb 1996 11:36:15 GMT
  6. Organization: DNV
  7. Message-ID: <4essvf$3ar@nms.telepost.no>
  8. References: <310DD74F.59E2@imacsg1.epfl.ch> <4env5h$3ai@nms.telepost.no> <3110887E.41C6@imacsg1.epfl.ch>
  9. NNTP-Posting-Host: hugin.sesam.dnv.no
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1 (Windows; I; 32bit)
  14.  
  15. Niels Hilbrink <Niels@imacsg1.epfl.ch> wrote:
  16. >Carsten Arnholm wrote:
  17. >> 
  18. >> Niels Hilbrink <Niels@imacsg1.epfl.ch> wrote:
  19. >
  20. >> >I have to link a piece of fortran code with c++ code but since if never
  21. >> >done that before I'd like to know a) can it be done and b) is it
  22. >> >difficult.
  23. >> >
  24. >> >Everything concerning this subject is welcome.
  25. >> 
  26. >> Hi there,
  27. >> 
  28. >> Mixed language programming (C++/FORTRAN 77) is not as hard as
  29. >> it may seem.
  30. >> 
  31. >> The only non-trivial problem you must solve is fortran CHARACTERs.
  32. >> You need to write a minimal CHARACTER class in C++.
  33. >> 
  34. >> If you require single-source portability, it becomes slightly
  35. >> more complicated, but not impossible.
  36. >> 
  37. >> Below follows a summary of some stuff that I have done, plus
  38. >> some additional thoughts.
  39. >
  40. >As understand it, this is the way to go if you want to call fortran routines 
  41. >in a c++ code. But what if you  want to do go the other way around ? 
  42. >
  43. >So how do I call an data member from a c++ class in fortran ?
  44. >say I want to call a member function called GetData() in fortran so
  45. >how do I transplate Class->GetData() in fortran ?
  46. >
  47. >Thanxs,
  48. >Niels
  49.  
  50.  
  51. Well, I have never done that, but generally I guess you would have to
  52.  
  53. a) Write a C-API (i.e. a set of C++ functions with C linkage) for the 
  54.    things you want to call from FORTRAN. Do this by declaring the API 
  55.    functions as extern "C" (avoids name mangling).
  56.  
  57. b) Provide some kind of pointer in the API to identify objects. These 
  58.    pointers are not used by FORTRAN, they're just object identifiers.
  59.  
  60.  
  61. Here's my suggestion (I don't know if it works).
  62.  
  63.  
  64. C++:
  65.  
  66.    class myClass {
  67.    public:
  68.        myMemFunc();
  69.    }
  70.  
  71.  
  72. C-API:
  73.  
  74.    extern "C" int myClass_create() 
  75.    {
  76.        myClass* myObject = new myClass;
  77.        return (int) (*myObject);
  78.    }
  79.  
  80.    extern "C" void myClass_myMemFunc(int& object)  
  81.    {
  82.        myClass* myObject = (myClass*) &object;
  83.        myObject->myMemFunc();
  84.    }
  85.  
  86.  
  87. FORTRAN:
  88.  
  89.     PROGRAM TEST
  90.     INTEGER MYOBJ
  91.     MYOBJ = myClass_create();
  92.     myClass_myMemFunc(MYOBJ)
  93.     END
  94.  
  95.  
  96. Anyone done this, or similar ?
  97.  
  98. Carsten
  99. ca@sesam.dnv.no
  100.  
  101.  
  102.